<?php

namespace app\hotel\controller;

use think\Controller;

class Coupon extends Controller
{
    protected $BusinessModel;
    protected $CouponmModel;
    protected $ReceiveModel;
    public function __construct()
    {
        parent::__construct();

        $this->BusinessModel = model('Business.Business');
        $this->CouponmModel = model('Hotel.Coupon.Coupon');
        $this->ReceiveModel = model('Hotel.Coupon.Receive');
    }

    public function index()
    {
        $list = $this->CouponmModel->where(['status' => 1])->order('createtime', 'desc')->select();

        if ($list) {
            $this->success('ok', null, $list);
            exit;
        } else {
            $this->error('无优惠活动');
            exit;
        }
    }

    public function info()
    {
        $cid = $this->request->param('cid', 0, 'trim');
        $busid = $this->request->param('busid', 0, 'trim');

        $info = $this->CouponmModel->where(['status' => 1, 'id' => $cid])->find();

        if ($info === false) {
            $this->error('未知优惠券');
            exit;
        }

        $business = $this->BusinessModel->where(['id' => $busid])->find();

        if ($business) {
            // 查找是否领取
            $check = $this->ReceiveModel->where(['cid' => $cid, 'busid' => $busid])->find();

            $info['receive'] = $check ? true : false;
        } else {
            $info['receive'] = false;
        }

        $receive = $this->ReceiveModel->with(['business'])->where(['cid' => $cid])->select();

        foreach ($receive as &$item) {
            $item = $item->toArray();

            $item['business'] = filter_property($item['business'], ['id', 'avatar_text', 'nickname']);
        }

        $data = [
            'coupon' => $info,
            'receive' => $receive,
        ];

        $this->success('返回优惠券信息', null, $data);
        exit;
    }

    // 领取
    public function receive()
    {
        $cid = $this->request->param('cid', 0, 'trim');
        $busid = $this->request->param('busid', 0, 'trim');

        $coupon = $this->CouponmModel->find($cid);

        if ($coupon === FALSE) {
            $this->error('暂无优惠券信息');
            exit;
        }

        if ($coupon['status'] == '0') {
            $this->error('活动已结束');
            exit;
        }

        if ($coupon['total'] <= '0') {
            $this->error('优惠券已经领完，无法领取');
            exit;
        }

        $business = $this->BusinessModel->find($busid);

        if (!$business) {
            $this->error('用户不存在');
            exit;
        }

        // 判断是否领取
        $check = $this->ReceiveModel->where(['cid' => $cid, 'busid' => $busid])->find();

        if ($check) {
            $this->error('已领取过优惠券，无法重复领取');
            exit;
        }

        // 开始事务
        $this->CouponmModel->startTrans();
        $this->ReceiveModel->startTrans();

        $receiveData = [
            'cid' => $cid,
            'busid' => $busid,
            'status' => '1'
        ];

        // 插入领取记录
        $receiveRes = $this->ReceiveModel->save($receiveData);

        if ($receiveRes === false) {
            $this->error($this->ReceiveModel->getError());
            exit;
        }

        // 更新优惠券数量
        $total = bcsub($coupon['total'], 1);
        $total = $total <= 0 ? 0 : $total;

        $couponData = [
            'id' => $cid,
            'total' => $total
        ];

        $couponRes = $this->CouponmModel->isUpdate(true)->save($couponData);

        if ($couponRes === false) {
            $this->ReceiveModel->rollback();
            $this->error($this->CouponmModel->getError());
            exit;
        }

        if ($receiveRes === false || $couponRes === false) {
            $this->CouponmModel->rollback();
            $this->ReceiveModel->rollback();
            $this->error('领取优惠券失败');
            exit;
        } else {
            $this->ReceiveModel->commit();
            $this->CouponmModel->commit();
            $this->success('领取优惠券成功');
            exit;
        }
    }

    // 优惠券列表
    public function list()
    {
        $busid = $this->request->param('busid', 0, 'trim');

        $list = $this->ReceiveModel
            ->with(['coupon'])
            ->where(['busid' => $busid, 'receive.status' => '1'])
            ->select();

        $this->success('ok', null, $list);
        exit;
    }
}
